package com.test.cities.controller;

import com.test.cities.vo.UserVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;

@Controller
public class IndexController {
    @GetMapping("/index")
    public String index(Model model){
        model.addAttribute("title","传递的Title");
        model.addAttribute("description","传递的description");
        model.addAttribute("keywords","传递的keywords");
        return "index";
    }

    @GetMapping("/basicTrain")
    public String basic(Model model){
        UserVO userVO = new UserVO();
        userVO.setId(30);
        userVO.setAge(21);
        userVO.setSex(1);
        userVO.setCreateTime(new Date());
        userVO.setTags(Arrays.asList("Java","PHP","Node"));
        userVO.setUsername("opp");
        userVO.setIsVip(true);
        model.addAttribute("user",userVO);
        model.addAttribute("htmlcontent","<b><i>连结</i><b/>");
        return "basic";
    }

    @GetMapping("/component")
    public String Compo(Model model){
        UserVO userVO = new UserVO();
        userVO.setUsername("AAA");
        model.addAttribute("user",userVO);
        return "component";
    }

    @GetMapping("/subscribe")
    public String form(@RequestParam(required = false) String exampleParam, HttpServletRequest request, HttpSession session){
        // 获取 ServletContext
        ServletContext servletContext = request.getServletContext();

        // 设置会话属性
        session.setAttribute("user", "exampleUser");

//        // 设置 Servlet 上下文初始化参数
        servletContext.setAttribute("appConfig", "exampleValue");

        return "subscribe";
    }

    @GetMapping("/date")
    public String date(Model model){
        model.addAttribute("today",new Date());
        return "date";
    }

    @GetMapping("remove")
    public String remove(Model model){
        Boolean isAdmin = true;
        model.addAttribute("isAdmin",isAdmin);
        return "remove";
    }

}
